#include "server.h"


int pipe[2]; //退出管道

int setnoblocking(int fd);
int epolladd(int epfd,int fd,bool enable_t);
int epolldel(int epfd,int fd);

void handler(int signum){
    printf("signum=%d\n",signum);
    write(pipe[1],"1",sizeof(char));
}

int main(int argc,char *argv[])
{   
    ARGS_CHECK(argc,3);

    //初始化资源
    bzero(pipe,sizeof(pipe));
    if(fork()>0){
        signal(SIGUSR1,handler);
        int pid = getpid();
        printf("父进程pid = %d\n", pid);
        wait(NULL);
        printf("Parent is going to exit!\n");
        exit(0);
    }
    close(pipe[1]);
    //1.共享队列
    ThreadPool_t* tpool=ThreadPool_init();
    //2.存放客户端信息，
    int fdArry[1024]={0};  //存放到来的客户端
    int fdtoindx[1024]={0};//获取根据netfd来获取fdArry下标
    int curidx=0;
    char buf[4096]={0};
    //socket
    int listenfd=socket(AF_INET,SOCK_STREAM,0);
    ERROR_CHECK(listenfd,-1,"socket");
    int reuse=1;
    setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse));
    struct sockaddr_in addr;
    addr.sin_family=AF_INET;
    inet_pton(AF_INET,argv[1],&addr.sin_addr);
    addr.sin_port=htons(atoi(argv[2]));
    int ret=bind(listenfd,(struct sockaddr*)&addr,sizeof(addr));
    ERROR_CHECK(ret,-1,"bind");
    ret=listen(listenfd,55);
    ERROR_CHECK(ret,-1,"listen");

    //epoll设置
    int epfd=epoll_create(1);
    epolladd(epfd,listenfd,1);
    struct epoll_event readyset[1024]={0};

    while(1){
        int readynums=epoll_wait(epfd,readyset,1024,-1);
        for(int i=0;i<readynums;i++){
            //新的客户端连接进入
            if(readyset[i].data.fd==listenfd){
                int netfd=accept(epfd,NULL,NULL);
                fdArry[curidx]=netfd;
                fdtoindx[netfd]=curidx;
                ++curidx;
                epolladd(epfd,netfd,0);//水平触发
            }else if(readyset[i].data.fd==pipe[0]){ // 
                
            }else{   //客户端发来信息
                TaskData_t *taskdata=(TaskData_t*)calloc(1,sizeof(TaskData_t));
                pthread_mutex_init(&taskdata->fd_mutex,NULL);
                pthread_cond_init(&taskdata->fd_cond,NULL);
                taskdata->netfd=readyset[i].data.fd;
                ssize_t sret=recv(readyset[i].data.fd,&taskdata->type,sizeof(int),MSG_WAITALL);
                if(sret==0){
                    int idx=fdtoindx[taskdata->netfd];
                    fdtoindx[taskdata->netfd]=0;
                    fdArry[idx]=0;
                    epolldel(epfd,taskdata->netfd);
                    free(taskdata);
                    break;
                }
                ssize_t sret=recv(taskdata->netfd,&taskdata->pathlen,sizeof(int),MSG_WAITALL);
                if(sret!=0) 
                recv(taskdata->netfd,taskdata->path,taskdata->pathlen,MSG_WAITALL);
                sret=recv(taskdata->netfd,&taskdata->assiglen,MSG_WAITALL);
                if(sret!=0)
                recv(taskdata->netfd,taskdata->assig,taskdata->assiglen,MSG_WAITALL);
                Task_t* task=Task_create(taskdata);
                pthread_mutex_lock(&tpool->mutex);
                TaskQue_push(tpool->taskque,task);
                pthread_mutex_unlock(&tpool->mutex);
                printf("发送给任务队列\n");
            }
        }
    }
    return 0;
}
int setnoblocking(int fd){
    int old_option=fcntl(fd,F_GETFL);
    int new_option=old_option|O_NONBLOCK;
    fcntl(fd,F_SETFL,new_option);
    return old_option;
}
int epolladd(int epfd,int fd,bool enable_t){ //bool 判断是否边缘触发
    struct epoll_event event;
    event.data.fd=fd;
    event.events=EPOLLIN;
    if(enable_t){
        event.events|=EPOLLET;
    }
    epoll_ctl(epfd,EPOLL_CTL_ADD,fd,&event);
    // setnonblocking(fd);
    return 0;
}
int epolldel(int epfd,int fd){
    epoll_ctl(epfd,EPOLL_CTL_DEL,fd,NULL);
    return 0;
}